package com.example.text8;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class PlayVideoActivity extends AppCompatActivity {
    @BindView(R.id.videoView)
    VideoView videoView;

    private static final String[] REQUEST_PERMISSIONS = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE};
    private static final int REQUEST_CODE = 123;
    public static final String VIDEO_PATH = "video_path";

    private String fileName;
    private MediaController mediaController;

    private Unbinder unbinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_play);

        unbinder = ButterKnife.bind(PlayVideoActivity.this);

        Intent intent = getIntent();
        fileName = intent.getStringExtra(VIDEO_PATH);

        mediaController = new MediaController(PlayVideoActivity.this);

        if (checkedPermission()) {
            playVideo();
        }
    }

    private boolean checkedPermission() {
        List<String> list = new ArrayList<>();
        for (String permission : REQUEST_PERMISSIONS) {
            if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
                list.add(permission);
            }
        }

        if (!list.isEmpty()) {
            requestPermissions(list.toArray(new String[0]), REQUEST_CODE);
            return false;
        } else {
            return true;
        }
    }

    @Override
    protected void onDestroy() {
        videoView.stopPlayback();

        if (unbinder != null) {
            unbinder.unbind();
        }

        super.onDestroy();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == REQUEST_CODE) {
            for (int result : grantResults) {
                if (result != PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(PlayVideoActivity.this, "获取权限失败", Toast.LENGTH_LONG).show();
                    return;
                }
            }

            playVideo();
        }
    }

    private void playVideo() {
        File videoFile = new File(fileName);
        if (videoFile.exists() && videoFile.isFile()) {
            mediaController.setMediaPlayer(videoView);

            videoView.requestFocus();
            videoView.setMediaController(mediaController);
            videoView.setOnCompletionListener(mp -> exit());
            videoView.setOnErrorListener((mp, what, extra) -> {
                exit();
                return false;
            });
            videoView.setOnPreparedListener(mp -> videoView.start());
            videoView.setOnInfoListener((mp, what, extra) -> {
                Log.i(PlayVideoActivity.this.getClass().getSimpleName(), "what=" + what + " extra=" + extra);
                return false;
            });
            videoView.setVideoPath(videoFile.getAbsolutePath());
        } else {
            Toast.makeText(PlayVideoActivity.this, "文件不存在", Toast.LENGTH_LONG).show();
        }
    }

    private void exit() {
        finish();
    }
}